/*
  @author NormanMises
  @date 2023/6/12 - 17:45
*/

package com.example.demo.service.impl.user.account;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.mapper.UserMapper;
import com.example.demo.pojo.user;
import com.example.demo.service.user.account.RegisterService;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class RegisterServiceImpl implements RegisterService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    PasswordEncoder passwordEncoder;
    @Override
    public Map<String,String> register(String username,String password,String confirmedpaassword){
        Map<String,String> map=new HashMap<>();
        if(username==null){
            map.put("error","用户名不为空");
            return map;
        }
        if(username.length()==0){
            map.put("error","用户名长度不能为空");
            System.out.println(map);
            return map;
        }
        if(password==""||confirmedpaassword==""){
            map.put("error","密码不为空");
            System.out.println(map);
            return map;
        }
        if(password.length()>100||confirmedpaassword.length()>100){
            map.put("error","密码长度不错超过100");
            System.out.println(map);
            return map;
        }
        if(username.length()>50){
            map.put("error","用户名长度不能大于50");
            System.out.println(map);
            return map;
        }
        if(!password.equals(confirmedpaassword)){
            map.put("error","密码不一致");
            System.out.println("密码"+password+" "+confirmedpaassword);
            return map;
        }
        username=username.trim();
        QueryWrapper<user> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("username",username);
        List<user> user=userMapper.selectList(queryWrapper);
        if(!user.isEmpty()){
            map.put("error","用户已存在");
            return  map;
        }
        String encodedPassword=passwordEncoder.encode(password);
        user user1=new user(null,username,encodedPassword);
        userMapper.insert(user1);
        map.put("message:","successed");
        return map;
    }
}
